package cn.yhz.pethome.dept.controller;

import cn.yhz.pethome.base.util.AjaxResult;
import cn.yhz.pethome.base.util.ResultBean;
import cn.yhz.pethome.dept.Query.DepartmentQuery;
import cn.yhz.pethome.dept.domain.Department;
import cn.yhz.pethome.dept.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department querydepartment(@PathVariable("id") Long id){
        return departmentService.selectById(id);
    }

    @GetMapping
    public List<Department> deptTree(){
        return departmentService.getTree();
    }

    /**
     * 查询所有高级查询
     * @param query
     * @return
     */
    @PostMapping
    public ResultBean<Department> querydepartment(@RequestBody DepartmentQuery query){
        return departmentService.queryAll(query);
    }

    /**
     * 根据id删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 新增和修改
     * @param department
     * @return
     */
    @PutMapping
    public AjaxResult insert(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.insert(department);
            }else{
                departmentService.update(department);
            }
           return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult dept(@RequestBody List<Long>ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


}
